﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;

namespace YJKRevitWndr
{
	public class DoorWinStatistics
	{
		public void StatisticsByDoc(Autodesk.Revit.DB.Document doc)
		{
			this.arResults.Clear();
			this.DoorStatistics(doc);
			this.WinStatistics(doc);
		}

		public void StatisticsByLevel(Autodesk.Revit.DB.Document doc, Level level)
		{
			this.arResults.Clear();
			this.DoorStatistics(doc, level);
			this.WinStatistics(doc, level);
		}

		private void DoorStatistics(Autodesk.Revit.DB.Document doc)
		{
			foreach (Level level in AssistFunc.GetAllLevel(doc))
			{
				this.DoorStatistics(doc, level);
			}
		}

		private void WinStatistics(Autodesk.Revit.DB.Document doc)
		{
			foreach (Level level in AssistFunc.GetAllLevel(doc))
			{
				this.WinStatistics(doc, level);
			}
		}

		private void DoorStatistics(Autodesk.Revit.DB.Document doc, Level level)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors);
			FilterElementIdRule filterElementIdRule = new FilterElementIdRule(new ParameterValueProvider(new ElementId(-1001352)), new FilterNumericEquals(), level.Id);
			filteredElementCollector.WherePasses(new ElementParameterFilter(filterElementIdRule, false));
			foreach (IGrouping<string, Element> grouping in filteredElementCollector.GroupBy(delegate(Element e)
			{
				Parameter parameter = ((FamilyInstance)e).get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS);
				if (parameter != null)
				{
					return string.Concat(new object[]
					{
						((FamilyInstance)e).Symbol.Family.Name,
						"|",
						Convert.ToDouble(((FamilyInstance)e).Symbol.get_Parameter(BuiltInParameter.CASEWORK_WIDTH).AsValueString()),
						"X",
						Convert.ToDouble(((FamilyInstance)e).Symbol.get_Parameter(BuiltInParameter.FAMILY_HEIGHT_PARAM).AsValueString()),
						"|",
						parameter.AsString()
					});
				}
				return null;
			}))
			{
				if (grouping != null)
				{
					this.arResults.Add(new DoorWinStatisticsResult(grouping.Key.Split(new char[]
					{
						'|'
					})[0], level.Name, Convert.ToDouble(grouping.Key.Split(new char[]
					{
						'|'
					})[1].Split(new char[]
					{
						'X'
					})[0]), Convert.ToDouble(grouping.Key.Split(new char[]
					{
						'|'
					})[1].Split(new char[]
					{
						'X'
					})[1]), (double)grouping.Count<Element>(), "樘", grouping.Key.Split(new char[]
					{
						'|'
					})[2]));
				}
			}
		}

		private void WinStatistics(Autodesk.Revit.DB.Document doc, Level level)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Windows);
			FilterElementIdRule filterElementIdRule = new FilterElementIdRule(new ParameterValueProvider(new ElementId(-1001352)), new FilterNumericEquals(), level.Id);
			filteredElementCollector.WherePasses(new ElementParameterFilter(filterElementIdRule, false));
			foreach (IGrouping<string, Element> grouping in filteredElementCollector.GroupBy(delegate(Element e)
			{
				Parameter parameter = ((FamilyInstance)e).get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS);
				if (parameter != null)
				{
					return string.Concat(new object[]
					{
						((FamilyInstance)e).Symbol.Family.Name,
						"|",
						Convert.ToDouble(((FamilyInstance)e).Symbol.get_Parameter(BuiltInParameter.CASEWORK_WIDTH).AsValueString()),
						"X",
						Convert.ToDouble(((FamilyInstance)e).Symbol.get_Parameter(BuiltInParameter.FAMILY_HEIGHT_PARAM).AsValueString()),
						"|",
						parameter.AsString()
					});
				}
				return null;
			}))
			{
				if (grouping != null)
				{
					this.arResults.Add(new DoorWinStatisticsResult(grouping.Key.Split(new char[]
					{
						'|'
					})[0], level.Name, Convert.ToDouble(grouping.Key.Split(new char[]
					{
						'|'
					})[1].Split(new char[]
					{
						'X'
					})[0]), Convert.ToDouble(grouping.Key.Split(new char[]
					{
						'|'
					})[1].Split(new char[]
					{
						'X'
					})[1]), (double)grouping.Count<Element>(), "樘", grouping.Key.Split(new char[]
					{
						'|'
					})[2]));
				}
			}
		}

		public List<DoorWinStatisticsResult> arResults = new List<DoorWinStatisticsResult>();
	}
}
